﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
////using System.Linq;
using System.Text;
using System.Xml;
using System.Security.AccessControl;
using System.Security.Principal;



namespace QuanLyKho.DataAccess
{
    public class clsConnect
    {
        //private string strConnectionString = "server=" + _Server + ";database=" + _CSDL + ";uid=" + _UID + ";pwd=" + _Pwd +"\"";
        private string strConnectionString = "";
        private string folder_C = @"C:\";
        private string folder_Local = @"localApp\QLK\";
        private string file = "ConnectionString.xml";

        private string urlFolder = "";

        public string UrlFolder
        {
            get { return urlFolder; }
            set { urlFolder = value; }
        }
        private string urlFile = "";

        public string UrlFile
        {
            get { return urlFile; }
            set { urlFile = value; }
        }

        private string serverName = "";

        public string ServerName
        {
            get { return serverName; }
            set { serverName = value; }
        }
        private string dbName = "";

        public string DbName
        {
            get { return dbName; }
            set { dbName = value; }
        }
        private string UserID = "";
        private string Pass = "";
        private bool type = false;
        /*
         * Constructor 
         */
        public clsConnect()
        {
            if (Directory.Exists(folder_C))
            {
                urlFolder = folder_C + folder_Local;
                urlFile = urlFolder + file;
            }

            if (File.Exists(urlFile))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(urlFile);
                XmlNodeList nList = doc.DocumentElement.ChildNodes;
                ServerName = nList[0]["ServerName"].InnerText;
                DbName = nList[0]["DbName"].InnerText;
                UserID = nList[0]["UserId"].InnerText;
                Pass = nList[0]["Password"].InnerText;
                if (nList[0]["ConnectType"].InnerText == "window Authenticate")
                    strConnectionString = "Server=" + ServerName + "; Database=" + DbName + "; Trusted_Connection = True;";
                else
                    strConnectionString = "Server=" + ServerName + "; Database=" + DbName + "; uid=" + UserID + "; pwd=" + Pass;
            }
        }
        /*
         * Property
         * Set and get a connection string
         */
        public string propertyConnectionString
        {
            set{
                strConnectionString = value;
            }
            get{
                return strConnectionString;
            }
        }

        public SqlConnection GetSqlConnection()
        {
            try{
                SqlConnection myConnection;
                myConnection = new SqlConnection(propertyConnectionString);
                return myConnection;
            }
            catch (Exception e){
                throw (new Exception(e.ToString()));
            }
        }

        public void Dispose()
        {

        }
    }
}
